<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserInsertRequest;
use App\User;
use Hash;
use DB;

class userController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) 
    {
        //
        $keywords = $request->input('keywords');
        // dd($keywords);die;
        if(empty($keywords)){
            $users =User::orderBy('id','desc')->paginate($request->input('num',10));
        }else{
            $users = User::orderBy('id','desc')->where('username','like','%'.$keywords.'%')->paginate($request->input('num',10));
        
        }
        
        return view('admin.user.index',['users'=>$users,'data'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        echo 2222;
        //显示添加页面
        return view('admin.user.create');
    }

    /**
     * 用户添加
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //用户数据验证
        $this->validate($request, [
                'username' => 'required|regex:/^\w{6,20}$/|unique:users,username',
                'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
                'email' =>'required|email',
            ],[
                'username.required'=>'对不起用户名必填',
                'username.regex'=>'用户名格式不正确',
                'username.unique'=>'用户名已经存在',
                'password.required' => '密码必填',
                'password.regex' => '密码格式不正确',
                'password.same' => '两次密码不一致',
                'email.required'=> '邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
            ]);

        $user = new User;
        $user -> username = $request->username;
        $user -> password = Hash::make($request->input('password'));
        $user -> email = $request->email;
        $user -> status = 1;
        // dd($user->age);
        if($request -> hasFile('img')){
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('img')->move($dirname,$name);
            //
            $user -> img = trim($dirname.$name,'.');
        }

        if($user->save()){
            return redirect('/user');
        }else{
            return back();
        }

    }
 
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $user = User::findOrFail($id);
        return view('admin.user.edit',['user'=>$user]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $user = User::findOrFail($id);
        $user -> username = $request->username;
        $user -> email = $request->email;
        $user -> status = 1;
        if($request -> hasFile('img')){
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('img')->move($dirname,$name);
            //
            $user -> img = trim($dirname.$name,'.');
        }

        if($user->update()){
            // echo 222;
            return redirect('/user');
        }else{
            // echo 3333;
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $user = User::findOrFail($id);
        // 获取用户信息
        $info = DB::table('users')->where('id','=',$id)->first();
        $path = $info->img;
        @unlink('.'.$path);
        if($user->delete()){
            return redirect('/user')->with('info','删除成功');
        }else{
            return back()->with('info','删除失败');
        }
    }

    public function aupdate(Request $request)
    {
        //获取状态
        $data = $request->only(['status']);
        //更新
        $res = DB::table('users')
            ->where('id','=',$request->input('id'))
            ->update($data);        
        if($res){
            return 1;
        }else{
            return 2;
        }
        
    }

    /**
     * 修改密码
     */
    public function changpass(Request $request)
    {
        return view('admin.user.changpass');
    }
    public function dochangpass(Request $request)
    {
        //获取用户id
        $id = session()->all()['username']['id'];
        //获取用户信息
        $user = DB::table('users')->where('id','=',$id)->first();
        
        if(empty($user)){
            abort(404);
        }
        //检测密码是否一致
        if(!Hash::check($request->input('password'), $user->password)){
            return back()->with('info','原密码不正确');//不一致返回
        }
        //若正确
        $this->validate($request, [
                'newpassword' => 'required|regex:/^\S{6,20}$/|same:repassword',
            ],[
            'newpassword.required'=>'密码不能为空',
            'newpassword.regex' => '密码格式不正确',
            'newpassword.same' => '两次密码不一致'
        ]);
        //执行更改
        $data['password'] = Hash::make($request->input('newpassword'));
        $res = DB::table('users')->where('id','=',$id)->update($data);
        if($res){
            return redirect('/admin')->with('info','修改成功');
        }else{
            return back()->with('info','修改失败');
        }
    }
}
